package com.bestcem.xm.ticket.service.impl;

import com.bestcem.xm.common.core.domain.message.SampleCopyMessage;
import com.bestcem.xm.ticket.service.*;
import com.bestcem.xm.ticket.service.dto.OrgCreatedMessage;
import com.bestcem.xm.ticket.service.dto.mq.*;
import com.bestcem.xm.ticket.util.TicketConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TicketMessageReceiverServiceImpl implements TicketMessageReceiverService {

    @Resource
    private TicketNotifyConfigService ticketNotifyConfigService;

    @Resource
    private TicketConfigUtil ticketConfigUtil;

    @Resource
    private TicketNotificationService ticketNotificationService;

    @Resource
    private TicketCopySample ticketCopySample;

    @Resource
    private TicketService ticketService;

    @Resource
    private DeliverAgainService deliverAgainService;

    @Resource
    private TemplateService templateService;

    /**
     * 测试完成
     * 检查工单配置,生成工单
     * survey.respondent.submit
     *
     * @param ticketConfigCheckDTO mq检查工单配置
     * @author jy.zhao
     * @date 2021/8/24 22:38
     **/
    @Override
    public void checkTicketConfig(TicketConfigCheckDTO ticketConfigCheckDTO) {
        ticketConfigUtil.validConfigAndCreateTicket(ticketConfigCheckDTO);
    }

    /**
     * 检查工单配置状态
     * survey.respondent.submit
     *
     * @param ticketConfigCheckDTO mq检查工单配置状态
     * @author jy.zhao
     * @date 2021/8/24 22:38
     **/
    @Override
    public void checkTicketConfigStatus(TicketConfigCheckDTO ticketConfigCheckDTO) {
        ticketConfigUtil.validTicketConfigAndDeliver(ticketConfigCheckDTO);
    }

    /**
     * 初步测试通过
     * 根据二次投放的答卷状态定时任务去更改工单状态
     *
     * @param ticketStatusUpdateDto mqDto
     * @author jy.zhao
     * @date 2021/8/24 22:38
     **/
    @Override
    public void ticketStatusUpdate(TicketStatusUpdateDto ticketStatusUpdateDto) {
        ticketService.updateTicketStatusAndDeliver(ticketStatusUpdateDto);
    }

    /**
     * 完成
     * 剩余接口调用，初步测试，可以走通
     * 触发通知提醒
     *
     * @param notificationTriggerDTO 通知触发dto
     * @author jy.zhao
     * @date 2021/8/26 14:55
     **/
    @Override
    public void notificationTrigger(NotificationTriggerDTO notificationTriggerDTO) {
        ticketNotificationService.triggerNotification(notificationTriggerDTO);
    }

    /**
     * 测试完成
     * 通过项目id创建通知
     *
     * @param notificationCreateDto mqDto
     * @author jy.zhao
     * @date 2021/8/24 22:42
     **/
    @Override
    public void createNotificationByProject(NotificationCreateDto notificationCreateDto) {
        ticketNotificationService.createDefaultNotification(notificationCreateDto.getId());
    }

    /**
     * 测试完成
     * 清理保存的已删除邮箱的id
     *
     * @param smtpIds 邮箱ids
     * @author jy.zhao
     * @date 2021/8/24 22:42
     **/
    @Override
    public void clearSmtp(List<String> smtpIds) {
        ticketNotifyConfigService.clearConfigSmtpId(smtpIds);
    }

    /**
     * 完成开发
     * 二次投放开始时接收mq触发答题状态条件，保存到RspdTimer
     *
     * @param deliverAgainDTO 再次投放mq
     * @author jy.zhao
     * @date 2021/8/24 22:42
     **/
    @Override
    public void deliverAgainBegin(DeliverAgainDTO deliverAgainDTO) {
        deliverAgainService.validDeliverAgainCondition(deliverAgainDTO);
    }

    /**
     * 复制工单
     *
     * @param
     * @author jy.zhao
     * @date 2021/9/6 17:56
     **/
    public Map<String, String> copySample(SampleCopyMessage copyMessage) {
        return ticketCopySample.ticketCopySample(copyMessage);
    }

    /**
     * 创建工单的详情模板和处理模板
     *
     * @param orgCreatedMessage 组织id
     * @author jy.zhao
     * @date 2021/9/7 11:01
     **/
    @Override
    public void createTicketDefaultTemplate(OrgCreatedMessage orgCreatedMessage) {
        templateService.createTicketDefaultTemplate(orgCreatedMessage.getId());
    }

    /**
     * 开启同步联系人，会根据题目中的手机号和邮箱新建或者更新联系人，
     * 当创建或者更新联系人之后把答卷信息更新到联系人足迹表（Java数据库）中
     * java创建联系人更新答卷之后，会发过来答卷的seq，Python这边查到答卷更新联系人足迹
     *
     * @param memberFootPrint 联系人足迹点
     * @author jy.zhao
     * @date 2021/9/16 10:50
     **/
    public void createFootPrint(MemberFootPrint memberFootPrint) {
        ticketService.createFootPrint(memberFootPrint);
    }
}